package cn.liye.pingfen.service.impl;

import cn.liye.pingfen.Util.SendSMSUtil;
import cn.liye.pingfen.exception.ServiceException;
import cn.liye.pingfen.mapper.UserMapper;
import cn.liye.pingfen.pojo.dto.UserLoginDTO;
import cn.liye.pingfen.pojo.dto.UserRegDTO;
import cn.liye.pingfen.pojo.dto.UserUpdateDTO;
import cn.liye.pingfen.pojo.entity.User;
import cn.liye.pingfen.pojo.vo.UserVO;
import cn.liye.pingfen.response.StatusCode;
import cn.liye.pingfen.service.IUserService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class UserService implements IUserService {
    @Autowired
    UserMapper mapper;

    @Override
    public void reg(UserRegDTO userRegDTO) {

        /**
         * 用户注册模块
         */
        UserVO userVO = mapper.selectByUsername(userRegDTO.getUsername());
        if (userVO != null) {
            throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
        }

        String encryptedPassword = DigestUtils.md5Hex(userRegDTO.getPassword());

        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user,"password");
        user.setPassword(encryptedPassword);
        user.setCreateTime(new Date());
        mapper.insert(user);
    }

    /**
     * 用户登录模块
     */

    @Override
    public UserVO login(UserLoginDTO userLoginDTO) {
        UserVO userVO = mapper.selectByPhoneNumber(userLoginDTO.getPhoneNumber());
        if (userVO == null) {
            throw new ServiceException(StatusCode.PHONE_NUMBER);
        }
        //  注册接口,存储用户密码需要先加密,后入库
        // 登录校验需要将用户手输明文密码加密后和数据库密文比较
        String password = userLoginDTO.getPassword();
        //password做加密
        String s = DigestUtils.md5Hex(password);
        if (!s.equals(userVO.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }



    @Override
    public UserVO loginsms(UserLoginDTO userLoginDTO) {
        UserVO userVO = mapper.selectByPhoneNumber(userLoginDTO.getPhoneNumber());
        if (userVO == null) {
            throw new ServiceException(StatusCode.PHONE_NUMBER);
        }
        return userVO;
    }

    @Override
    public void update(UserUpdateDTO userUpdateDTO) {
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
        String encryptedPassword = DigestUtils.md5Hex(userUpdateDTO.getPassword());

        BeanUtils.copyProperties(userUpdateDTO, user,"password");
        user.setPassword(encryptedPassword);
        mapper.update(user);
    }


    @Override
    public UserRegDTO sms(UserRegDTO userRegDTO) {

        /**
         * 获取验证码模块
         */

        // 创建一个Random对象
        Random random = new Random();
        // 生成一个1000到9999之间的随机整数
        int captcha = random.nextInt(9000) + 1000;

        userRegDTO.setCaptcha(captcha);

        long timeStamp = System.currentTimeMillis();
        String content = "【企业评分网】您的验证码：" + captcha + "该验证码5分钟内有效，请勿泄露于他人"; // 验证码


        String userName = "石家庄立业软件";
        String password = "KyJmQx6QEGDv";
        String sign = DigestUtils.md5Hex(userName + content + timeStamp + DigestUtils.md5Hex(password)).toLowerCase();
        String phoneNumber = userRegDTO.getPhoneNumber();

        String url = "http://api.shantuoke.com:8001/sms/api/sendMessage";

        List<String> phoneList = new ArrayList<>();
        phoneList.add(phoneNumber);


        JSONObject json = new JSONObject();
        json.put("userName", userName);
        json.put("content", content);
        json.put("timestamp", timeStamp);
        json.put("phoneList", phoneList);
        json.put("sign", sign);

        try {
            String post = SendSMSUtil.sendHttp(url, "POST", json.toString(), "UTF-8");
            System.out.println(post);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return userRegDTO;
    }


}
